package com.wandian.audio.mvvm.audiotoprankdetials;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.bean.MusicTopRankListBean;
import com.wandian.audio.data.BaseAppApi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description AudioTopRankDetailsViewModel
 **/
public class AudioTopRankDetailsViewModel extends BaseViewModel {
    public MutableLiveData<List<MusicTopRankListBean.PlaylistEntity.TracksEntity>> listData = new MutableLiveData();
    public AudioTopRankDetailsViewModel(@NonNull Application application) {
        super(application);
    }

    public void getMusicTopRankDetails(String id){
        Log.d("getMusicTopRankDetails",id+"");
        showDialog();
        //创建Retrofit对象
        Retrofit retrofit = RetrofitUtil.createMusicRetrofit();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        Call<Object> call = request.getMusicTopListDetails(map);
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                dismissDialog();
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                Log.d("getMusicTopListDetails", "getMusicTopListDetails="+ JsonUtil.toJson(response.body()));
                if(UIUtils.isEmpty(response.body())){
                    return;
                }
                MusicTopRankListBean bean = (MusicTopRankListBean) JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()),MusicTopRankListBean.class);
                if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getPlaylist()) && !UIUtils.isEmpty(bean.getPlaylist().getTracks()) && bean.getPlaylist().getTracks().size()>0){
                    listData.setValue(bean.getPlaylist().getTracks());
                }else {
                    ToastUtil.show("网络错误");
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                dismissDialog();
                ToastUtil.show("网络错误");
                Log.d("getMusicTopListDetails", "error="+t.getMessage());
            }
        });

    }
}
